package com.iweb.service;

import com.iweb.mapper.BookMapper;
import com.iweb.mapper.PlaceMapper;
import com.iweb.pojo.Book;
import com.iweb.pojo.Place;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional(propagation = Propagation.REQUIRED)
public class BookServiceImp implements BookService {
    @Autowired
    BookMapper bookMapper;

    @Autowired
    PlaceMapper placeMapper;

    @Override
    public void insertBook(Book book , Place place) {
        place.setStatus("已被预定");
        placeMapper.updatePlaceStatus(place);
        bookMapper.insert(book);
    }
    @Override
    public Boolean updateBook(Book book) {
        return bookMapper.update(book)>0;
    }

    public Boolean deleteBook(Book book) {

        return bookMapper.delete(book)>0;
    }
    @Override
    public List<Map> selectBookByStartTime(Book book) {
        return bookMapper.select(book);
    }

    @Override
    public List selectBookByUserID(int userID) {
        return bookMapper.selectByUserID(userID);
    }

    @Override
    public boolean checkBookByDate(Book book) {

        return bookMapper.checkBookByDate(book)==null;
    }

    public Boolean deleteBook(Book book,Place place) {
        place.setStatus("未预定");
        placeMapper.updatePlaceStatus(place);
        return bookMapper.delete(book)>0;
    }
}
